package com.zcp.week3;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.zcp.week3.adapter.MyAda;
import com.zcp.week3.bean.Bean;
import com.zcp.week3.bean.Data;

import java.io.IOException;
import java.util.ArrayList;



public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button bt_clear;
    private Button bt_dingwei;
    private RecyclerView recycle;
    public static  DisplayImageOptions options;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        initView();
        getData();
    }

    private void initView() {
        bt_clear = (Button) findViewById(R.id.bt_clear);
        bt_dingwei = (Button) findViewById(R.id.bt_dingwei);
        recycle = (RecyclerView) findViewById(R.id.recycle);
        bt_dingwei.setOnClickListener(this);
        bt_clear.setOnClickListener(this);
    }

    //请求数据
    public void getData() {
        //创建okHttpClient对象
        OkHttpClient mOkHttpClient = new OkHttpClient();
        //创建一个Request
        final Request request = new Request.Builder()
                .url("http://www.yulin520.com/a2a/impressApi/news/mergeList?sign=C7548DE604BCB8A17592EFB9006F9265&pageSize=10&gender=2&ts=1871746850&page=1")
                .build();
        //new call
        Call call = mOkHttpClient.newCall(request);
         //请求加入调度
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
            }

            @Override
            public void onResponse(final Response response) throws IOException {
                final String htmlStr =  response.body().string();
                runOnUiThread(new Runnable() {

                    private ArrayList<Data> list;

                    @Override
                    public void run() {
                        Gson gson=new Gson();
                        Bean bean = gson.fromJson(htmlStr, Bean.class);
                        list = bean.data;
                        //Log.d("zzz",list.toString());
                        LinearLayoutManager manager=new LinearLayoutManager(MainActivity.this);
                        recycle.setLayoutManager(manager);
                        //设置适配器
                        recycle.setAdapter(new MyAda(MainActivity.this,list));
                    }
                });
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.bt_dingwei:
                Intent intent=new Intent(MainActivity.this,MapActivity.class);
                startActivity(intent);
                break;
            case R.id.bt_clear:
                ImageLoader.getInstance().clearDiskCache();
                Toast.makeText(MainActivity.this,"清除成功",Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
